package com.lcy.app.api.http;

import android.os.Build;

import androidx.appcompat.app.AppCompatActivity;

import com.lcy.app.api.domain.RespCode;
import com.lcy.app.api.domain.RespEntity;
import com.lcy.app.common.util.StrUtil;
import com.lcy.app.common.util.ToastUtil;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class HTTP<T> {
    public static final String BASE_URL = "http://app.lucongyang.cn:8011/api/";

    /**
     * 同步阻塞
     */
    public RespEntity<T> synRequest(AppCompatActivity appCompatActivity, ApiCall<T> apiCall) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        Call<RespEntity<T>> call = apiCall.call(retrofit);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            CompletableFuture<RespEntity<T>> completableFuture = CompletableFuture.supplyAsync(() -> {
                // 请求
                Response<RespEntity<T>> respEntityResponse = null;
                try {
                    respEntityResponse = call.execute();
                } catch (IOException e) {
                    e.printStackTrace();
                    return new RespEntity<>(RespCode.SERVER_ERROR);
                }

                // 处理结果
                if (respEntityResponse.code() == RespCode.SUCCESS.getCode()) {
                    if (respEntityResponse.body() != null) {
                        System.out.println(respEntityResponse.body().getData());
                        return respEntityResponse.body();
                    }
                } else {
                    okhttp3.Response response = respEntityResponse.raw();
                    if (StrUtil.isValidWithoutBlank(response.message())) {
                        return new RespEntity<>(RespCode.CUSTOM_ERROR.getCode(), response.message());
                    }
                }
                return new RespEntity<>(RespCode.SERVER_ERROR);
            });
            RespEntity<T> respEntity = completableFuture.join();

            if (respEntity.getCode() == RespCode.CUSTOM_ERROR.getCode()) {
                ToastUtil.error(appCompatActivity, respEntity.getMessage());
            }

            return respEntity;
        }

        return new RespEntity<>(RespCode.SERVER_ERROR);
    }

    /**
     * 异步非阻塞
     * 待封装
     */
    public RespEntity<T> aycRequest(ApiCall<T> apiCall) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        Call<RespEntity<T>> call = apiCall.call(retrofit);

        try {
            call.enqueue(new Callback<RespEntity<T>>() {
                @Override
                public void onResponse(Call<RespEntity<T>> call, Response<RespEntity<T>> response) {
                    System.out.println(response.body());
                }

                @Override
                public void onFailure(Call<RespEntity<T>> call, Throwable t) {
                    System.out.println(t.getMessage());
                }
            });
        } catch (Exception e) {
            throw new RuntimeException("失败");
        }
        return null;
    }


}
